package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class OrderWebController {

    @Resource
    OrderService orderService;


    /**
     * 生成订单确认页
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpServletRequest request) throws Exception{
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        //展示订单确认的数据
        model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }

    /**
     * 下单功能
     * 在订单确认页面点击订单提交会将订单信息封装到OrderSubmitVo对象中，进行Post提交
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo){
        SubmitOrderResponseVo vo = orderService.submitOrder(orderSubmitVo);
        //去创建订单、验令牌、锁库存。。。
        //下单成功来到支付选择页
        //失败则回到订单确认页重新确认订单信息
        if(vo != null){
            if(vo.getCode() == 0){
                return "pay";
            }else {
                return "redirect://http://cart.gulimall.com/toTrade";
            }
        }
        return "redirect://http://cart.gulimall.com/toTrade";
    }

}
